(function($, window){

    var debug = function(title, msg){
        // return true;
        console.log(title);
        console.log(msg);
    }

	var GameSdkServer = function(){
	}

	//支付引导模板id
    GameSdkServer.payGuideId = '#payment-page';
	//当前支付参数
	GameSdkServer.payParams = {};
	//是否是试玩用户
    GameSdkServer.isOneKeyUser = bh_api_config.IS_ONEKEY_USER;

	//cp初始化
    GameSdkServer.init = function() {
		//初始化分享参数
	};

    //sdk切换账号
    GameSdkServer.logout = function(){
        window.location.href = bh_api_config.API_LOGOUT_URL + '&redirect_url=' + window.location.href;
    }

    /**
     * 显示支付页面
     * @param payParams
     */
	GameSdkServer.pay = function(payParams){
        var payQueryString = $.param(payParams);
      //  console.log(payQueryString);
        window.location.href = bh_api_config.API_PAY_URL+"&"+payQueryString; 
        
        /*
          GameSdkServer.payParams = payParams;
	    //check onekey user
        if( GameSdkServer.isOneKeyUser === true){
            var $bindWrap = $(".binding-mp");
            $bindWrap.show();
            return false;
        }

	    var $productName = $(GameSdkServer.payGuideId + " .product-name");
	    var $productFee = $(GameSdkServer.payGuideId + " .product-fee");

	    $productName.text(payParams.product_name);
        $productFee.text(payParams.fee);

        var payQueryString = $.param(payParams);
        //数据绑定到元素
        $(GameSdkServer.payGuideId + " .pay-btn").data('payurl', payQueryString);

		$(GameSdkServer.payGuideId).show();*/
	}

    /**
     * 一键完善后重新发起支付
     */
	GameSdkServer.onkeyImproveRePay = function(){
        GameSdkServer.isOneKeyUser = false;
	    GameSdkServer.pay(GameSdkServer.payParams);
    }

    /**
     * 显示微信分享引导手指
     */
    GameSdkServer.showWxShareGuide = function(){
        $('#my-share').show();
        $('#share-overspread').show();
    }

    /**
     * 隐藏微信分享引导手指
     */
    GameSdkServer.hideWxShareGuide = function(){
        $('#my-share').hide();
        $('#share-overspread').hide();
    }

    /**
     * 分享
     */
    GameSdkServer.share = function(){
        var ua = navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == "micromessenger") {
            //显示遮罩
            GameSdkServer.showWxShareGuide();
        }else if (ua.match(/qiwanwapp/i) == "qiwanwapp") {
            if(!gameShareInfo){
                alert('gameShareInfo信息为空');
                return false;
            }
            //通知客户端分享。  客户端分享后调用GameSdkServer.shareSuccessCallback(gameShareInfo)通知分享成功。
            var gameShareInfoString = JSON.stringify(gameShareInfo);
            QiwanwAppBrige.share(gameShareInfoString);
        } else {
            //alert('暂不支持pc端分享');
            $('#share-qr').show();
            $(".public-number-cross").one('click', function () {
                $("#share-qr").hide();
            });
        }
    }

    /**
     * 分享成功回调
     * @param shareInfo
     */
    GameSdkServer.shareSuccessCallback = function(shareInfo){

        GameSdkServer.hideWxShareGuide();

        if(typeof shareInfo == 'string'){
            shareInfo = JSON.parse(shareInfo);
        }

        var game_if_name = 'game_if';
        //往iframe里传递消息
        var event = 'share_success_back';
        document.getElementById(game_if_name).contentWindow.postMessage({'event':event}, '*');

        //report share event
        $.ajax({
            type: "get",
            data: {'shareInfo' : shareInfo},
            url: bh_api_config.API_REPORT_SHARE_SUCCESS_URL,
            dataType: "jsonp",
            jsonp:'callback',
            success: function(respon){
                console.log('QW:分享事件上报：' + respon.message);
            }
        });//
    }

    /**
     * 显示关注
     */
    GameSdkServer.showFollow = function () {
        $("#public-number").show();
        var $popBox = $(".public-number-cross");
        $popBox.one('click', function () {
            $("#public-number").hide();
        });
    }

	window.GameSdkServer = GameSdkServer;

	//监听消息
    var receive = function(e){
        debug('接受cp请求数据', e.data);
        var event = e.data.event;
		//支付
        if( event == 'bh_pay' ){
            GameSdkServer.pay(e.data.payParams);
        }else if( event == 'bh_logout'){
            GameSdkServer.logout();
        }else if( event == 'bh_weixinfollow'){
            GameSdkServer.showFollow();
        }else if( event == 'bh_share'){
            GameSdkServer.share();
        }
    }

	//监听消息
    window.addEventListener('message', receive, false);

})(jQuery, window);

